package com.zhs.demoutil.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhs.demoutil.dao.BaseCheckResultDao;
import com.zhs.demoutil.entity.BaseCheckResult;
import com.zhs.demoutil.listener.BaseCheckResultListener;
import com.zhs.demoutil.service.BaseCheckResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@Service
public class BaseCheckResultServiceImpl extends ServiceImpl<BaseCheckResultDao, BaseCheckResult> implements BaseCheckResultService {
    @Override
    public void addByExcel(MultipartFile file) {
        String path = "/tmp/file/"+System.currentTimeMillis()+"/"+file.getOriginalFilename();
        try {
            File dest = new File(path);
            if(!dest.exists()){
                dest.mkdirs();
            }
            file.transferTo(dest);
        } catch (IOException e) {
            log.error("文件读取失败");
            throw new RuntimeException("文件读取失败",e);
        }
        EasyExcel.read(path, BaseCheckResult.class, new BaseCheckResultListener(this)).sheet().doRead();
    }

    @Override
    public void addByExcel(String file) {
        EasyExcel.read(file, BaseCheckResult.class, new BaseCheckResultListener(this)).sheet().doRead();
    }
}
